package com.giveme5.util.validation;

import java.lang.annotation.Annotation;

import org.nutz.mvc.ActionContext;
import org.nutz.mvc.impl.processor.AbstractProcessor;

import com.giveme5.util.validation.annotation.AnnotationValidation;
import com.giveme5.util.validation.annotation.annotations.Valid;
import com.giveme5.util.validation.annotation.annotations.Validations;

/**
 * 可用于 MVC 效验的动作链
 * <p>
 * 要求 action 参数中必须有一个 Errors 类型的参数（不需要使用 Param 声明）。当验证完成后会向这个参数赋值
 * 
 * @author QinerG(QinerG@gmail.com)
 */
public class ValidationProcessor extends AbstractProcessor {

	private static AnnotationValidation av = new AnnotationValidation();

	public void process(ActionContext ac) throws Throwable {
		Errors es = ValidationUtils.checkArgs(ac.getMethod().getParameterTypes(), ac.getMethodArgs());
		// 参数中没找到 Errors 类型的参数，忽略处理，继续其它动作链处理
		if (null != es) {
			Object[] args = ac.getMethodArgs();
			for (int i = 0; i < args.length; i++) {
				Annotation[] annotations = ac.getMethod().getParameterAnnotations()[i];
				for (Annotation annotation : annotations) {
					if (annotation instanceof Valid) {
						av.validate(args[i], es);
						break;
					} else if (annotation instanceof Validations) {
						av.validateParam(null, args[i], (Validations) annotation, es);
					}
				}
			}
		}
		doNext(ac);
	}
}
